<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<button id="btn_send">向服务器发送信息</button>

<script>

  // code 1 2 3 4 5 6

  let wsClient = new WebSocket("ws://localhost:8022");
  console.log(wsClient);

  btn_send.onclick = function () {
    wsClient.send(JSON.stringify({
      code: 1,
      msg: '你好啊 服务器'
    }))
  };

  wsClient.onopen = () => {
    console.log('已连接');
  };

  wsClient.onmessage = (msg) => {
    console.log('已连接');
    let {data} = msg;
    console.log(JSON.parse(data));


  };


</script>
</body>
</html>
